package com.magist.manage.web.service.impl;

import com.magist.core.constants.Deleted;
import com.magist.core.service.SimpleServiceImpl;
import com.magist.manage.enums.ValidStatusEnum;
import com.magist.manage.web.dao.MailAcceptUsersMapper;
import com.magist.manage.web.entity.MailAcceptUsers;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.entity.UserLogin;
import com.magist.manage.web.service.MailAcceptUsersService;
import com.magist.manage.web.service.UserInfoService;
import com.magist.manage.web.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class MailAcceptUsersServiceImpl extends SimpleServiceImpl<MailAcceptUsers> implements MailAcceptUsersService{

	@Autowired
	private MailAcceptUsersMapper mailAcceptUsersMapper;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private UserLoginService userLoginService;

	@Override
	public void insertMailUsers(String[] userIds) {
		if (userIds ==null || userIds.length <1) {
		    return ;
		}
		for (String userId : userIds) {
			UserInfo userInfo = userInfoService.getById(userId);
			UserLogin userLogin = userLoginService.getById(userId);
			if (ObjectUtils.isEmpty(userInfo) || ObjectUtils.isEmpty(userLogin)) {
				continue;
			}
			String email = userLogin.getEmail();
			if(StringUtils.isEmpty(email)) {
				continue;
			}
			// 查询是否添加过
			MailAcceptUsers entity = new MailAcceptUsers();
			entity.setUserId(userId);
			entity.setStatus(ValidStatusEnum.VALID.getCode());
			List<MailAcceptUsers> mailUsers = this.find(entity);
			if (!CollectionUtils.isEmpty(mailUsers)) {
			 	continue;
			}
			MailAcceptUsers mailAcceptUsers = new MailAcceptUsers();
			mailAcceptUsers.setUserId(userId);
			mailAcceptUsers.setUserName(userInfo.getUserName());
			mailAcceptUsers.setMailAddress(userLogin.getEmail());
			mailAcceptUsers.setStatus(ValidStatusEnum.VALID.getCode());
            mailAcceptUsers.setId(this.getNewId());
			this.insert(mailAcceptUsers);
		}

	}

	@Override
	public List<MailAcceptUsers> findAll() {
		MailAcceptUsers temp = new MailAcceptUsers();
		temp.setDeleted(Deleted.NO_STR);
		return  this.find(temp);

	}
}